/*
 * ValidacionIngresos.java
 *
 * Created on 29 de junio de 2007, 12:30 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package escr.clases;

import escr.persistencia.Paciente;
import escr.persistencia.Turno;
import escr.persistencia.Usuario;
import escr.persistencia.Usuturno;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author USER_2007
 */
public class ValidacionIngresos {
    
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("WebAdtafa_EcPU");
    
    /** Creates a new instance of ValidacionIngresos */
    public ValidacionIngresos() {
    }
    
    public Usuturno iniciarSesion(String login, String pasword) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object u=null;
        try {
            u = em.createQuery("SELECT u FROM Usuturno u where u.ustLogin =:login and u.ustPassword =:pasword and u.ustEstado=1").setParameter("login",login).setParameter("pasword", pasword).getSingleResult();
            if (u==null) {
                return null;
            }
            return (Usuturno) u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
    
//    public Presupuesto comparaDocumento(Integer cod) {
//        EntityManager em = emf.createEntityManager();
//        em.getTransaction().begin();
//        Object u=null;
//        try {
//            u = em.createQuery("SELECT e FROM Presupuesto e where e.docCod =:cod").setParameter("cod",cod).getSingleResult();
//            if (u==null) {
//                return null;
//            }
//            return (Presupuesto) u;
//        } catch (Exception e) {
//            e.printStackTrace();
//            em.getTransaction().rollback();
//        } finally {
//            em.close();
//        }
//        return null;
//    }
    
    //Coprobacion dela existencia de 
    public List buscarNPaciente(Paciente pac, Date d1) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        List  u=null;
        try {
            u = em.createQuery("SELECT e FROM Turno e where e.pacCodigo =:pac and e.turCita =:d1").setParameter("pac", pac).setParameter("d1",d1).getResultList();
            if (u.isEmpty()) {
                return null;
            }
            return  u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
    
 public Paciente GetPaciciente(Integer Cod){
        
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object u=null;        
             try {
            // TODO:
            u = em.createQuery("SELECT p FROM Paciente p WHERE p.pacCodigo = :pacCodigo").setParameter("pacCodigo",Cod).getSingleResult();
            if (u==null) {
                return null;
            }
            return (Paciente) u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
     
 }
    
    
    public Usuario confirmarLogin(String login) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object u=null;
        try {
            // TODO:
            u = em.createQuery("SELECT e FROM Usuario e where e.usrLogin =:login").setParameter("login",login).getSingleResult();
            if (u==null) {
                return null;
            }
            return (Usuario) u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
    
    
    public Usuturno confirmarLoginTurno(String login) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object u=null;
        try {
            // TODO:
            u = em.createQuery("SELECT e FROM Usuturno e where e.ustLogin =:login").setParameter("login",login).getSingleResult();
            if (u==null) {
                return null;
            }
            return (Usuturno) u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
    public Usuario confirmarTipoU(String login) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object u=null;
        try {
            u = em.createQuery("SELECT e.tpuCodigo FROM Usuario e where e.usrLogin =:login").setParameter("login",login).getSingleResult();
            if (u==null) {
                return null;
            }
            return (Usuario) u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
    
    public Integer comparaTUsua(Object login) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object u=null;
        try {
            u = em.createQuery("SELECT e.tpuCodigo FROM Usuario e where e.usrLogin =:login").setParameter("login",login).getSingleResult();
            if (u==null) {
                return null;
            }
            return (Integer) u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
    
   
    
    public void persist(Object object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            // TODO:
            // em.persist(object);    em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    
}
